/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright (c) 2013-2014 sagyf Yang. The Four Group.
 */

package app.controllers.admin.booking;

import app.models.DaoKit;
import app.models.booking.DiningCategory;
import app.models.booking.DiningMarket;
import app.models.booking.DiningNums;
import app.models.booking.DiningRecord;
import app.models.booking.DiningSetting;
import app.models.booking.DiningTime;
import app.models.brand.Brand;
import com.github.sog.controller.AjaxMessage;
import com.github.sog.controller.BasicController;
import com.github.sog.controller.datatables.core.DatatablesCriterias;
import com.github.sog.kit.date.DateProvider;
import com.github.sog.libs.AppFunc;
import com.google.common.base.Strings;
import com.google.common.collect.Maps;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * .
 * </p>
 *
 * @author sagyf yang
 * @version 1.0 2014-05-26 23:04
 * @since JDK 1.6
 */
public class DiningController extends BasicController {

    public static final String ADMIN_BOOKING_DINING = "/admin/booking/dining";
    public static final String BRAND                = "brand";

    /**
     * The index route.
     */
    public void index() {
        render("index.ftl");
    }

    public void list() {
        final DatatablesCriterias criterias = getCriterias();
        Page<Record> parkingPage = Brand.dao.datatableWithDining(criterias);

        renderDataTables(parkingPage, criterias);
    }


    public void time() {
        long brand = getParaToLong(0, 0l);
        if (brand == 0) {
            redirect(ADMIN_BOOKING_DINING);
            return;
        }
        DiningTime diningTime = DiningTime.dao.findByBrand(brand);
        if (diningTime == null) {
            diningTime = new DiningTime();
            final DateTime now = DateTime.now();
            diningTime.set("start_time", now.toDate());
            diningTime.set("end_time", now.plusDays(1).toDate());
            diningTime.set("brand", brand);
        }
        setAttr(BRAND, Brand.dao.findById(brand));
        setAttr("time", diningTime);
        render("time.ftl");
    }


    public void time_save() {
        DiningTime diningTime = getModel(DiningTime.class);

        diningTime.set("start_time", DateTime.parse(getPara("begin_time"), AppFunc.DATE_TIME_PATTERN_YYYY_MM_DD_HH_MM).toDate());
        diningTime.set("end_time", DateTime.parse(getPara("end_time"), AppFunc.DATE_TIME_PATTERN_YYYY_MM_DD_HH_MM).toDate());
        if (DaoKit.isNew(diningTime)) {
            diningTime.save();
        } else {
            diningTime.update();
        }
        redirect(ADMIN_BOOKING_DINING);
    }

    public void nums() {
        long brand = getParaToLong(0, 0l);
        if (brand == 0) {
            redirect(ADMIN_BOOKING_DINING);
            return;
        }
        List<DiningNums> nums = DiningNums.dao.findByBrand(brand);
        final Map<Integer, DiningNums> group_nums = Maps.newHashMap();
        for (DiningNums num : nums) {
            group_nums.put(num.getInt("catetory"), num);
        }
        List<DiningCategory> categories = DiningCategory.dao.findByBrand(brand);
        Map<String, DiningNums> categoryDiningNumsMap = Maps.newHashMap();
        DiningNums num_category;
        for (DiningCategory category : categories) {
            num_category = group_nums.get(category.getInt(AppFunc.TABLE_PK_COLUMN));
            if (num_category == null) {
                num_category = new DiningNums();
                num_category.set("catetory", category.getNumber(AppFunc.TABLE_PK_COLUMN));
                num_category.set("brand", brand);
                num_category.set("num", 1);
            }
            categoryDiningNumsMap.put(category.getStr("name"), num_category);
        }
        setAttr(BRAND, Brand.dao.findById(brand));
        setAttr("group_items", categoryDiningNumsMap);
    }

    public void num_save() {
        String[] brans = getParaValues("brand");
        String[] ids = getParaValues("id");
        String[] nums = getParaValues("num");
        String[] catetorys = getParaValues("catetory");
        for (int i = 0; i < nums.length; i++) {
            String num = StringUtils.defaultIfEmpty(nums[i], "1");
            String id = ids[i];
            String brand = brans[i];
            String catetory = catetorys[i];
            // todo check
            DiningNums diningNums = new DiningNums();
            diningNums.set("brand", brand);
            diningNums.set("num", num);
            diningNums.set("catetory", catetory);
            diningNums.set("create_time", DateProvider.DEFAULT.getCurrentDate());
            if (Strings.isNullOrEmpty(id)) {
                diningNums.save();
            } else {
                diningNums.set("id", id);
                diningNums.update();
            }
        }
        redirect(ADMIN_BOOKING_DINING);
    }

    public void member() {
        long brand = getParaToLong(0, 0l);
        if (brand == 0) {
            redirect(ADMIN_BOOKING_DINING);
            return;
        }

        DiningSetting setting = DiningSetting.dao.findByBrand(brand);
        if (setting == null) {
            setting = new DiningSetting();
            setting.set("brand", brand);
            setting.set("oper", -1);
            setting.set("currency", 5);
        }
        setAttr("setting", setting);
        setAttr(BRAND, Brand.dao.findById(brand));
    }

    public void member_save() {
        DiningSetting setting = getModel(DiningSetting.class);
        if (DaoKit.isNew(setting)) {
            setting.set("create_time", DateProvider.DEFAULT.getCurrentDate());
            setting.save();
        } else {
            setting.update();
        }
        redirect(ADMIN_BOOKING_DINING);
    }

    public void info() {
        long brandId = getParaToLong(0, 0l);
        if (brandId == 0) {
            redirect(ADMIN_BOOKING_DINING);
            return;
        }
        Brand brand = Brand.dao.findById(brandId);
        setAttr(BRAND, brand);
    }

    public void info_save() {
        Brand brand = getModel(Brand.class);
        if (!DaoKit.isNew(brand)) {

            brand.set("booking", BooleanUtils.toBooleanObject(getPara("booking", "off")));
            brand.update();
        }
        redirect(ADMIN_BOOKING_DINING);
    }

    public void record() {
        List<Brand> brandList = Brand.dao.findByDining();//品牌
        setAttr("brandList", brandList);
        render("record.ftl");
    }

    public void brand_marker_category() {
        final long brandId = getParaToLong(0, 0l);
        Map<String, Object> map = Maps.newHashMap();
        if (brandId > 0) {
            final List<DiningCategory> categories = DiningCategory.dao.findByBrand(brandId);
            final List<DiningMarket> markets = DiningMarket.dao.findByBrand(brandId);

            map.put("category", categories);
            map.put("market", markets);
            renderJson(AjaxMessage.ok(map));
        } else {
            renderJson(AjaxMessage.nodata());
        }
    }

    public void filter() {
        final DatatablesCriterias criterias = getCriterias();
        String brand = getPara("brand");
        String voucher = getPara("voucher");
        String phone = getPara("phone");

        String start = getPara("start");
        String end = getPara("end");

        int market = getParaToInt("market", 0);
        int category = getParaToInt("category", 0);
        Page<DiningRecord> pageRt = DiningRecord.dao.datatable(criterias, brand, voucher, phone, start, end, market, category);

        renderDataTables(pageRt, criterias);
    }
}